package com.jt.controller;

import com.jt.pojo.ItemCat;
import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/findAll")
    public List<User> findAll(){
        return userService.findAll();
    }
    //登录
    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
         String token=userService.login(user);
         if(token==null || "".equals(token)){
             return SysResult.fail();
         }
         return SysResult.success("服务器调用成功",token);
    }
    //获取用户列表
    @GetMapping("/list")
    public SysResult getUserList(PageResult pageResult){
        pageResult=userService.getUserList(pageResult);
        return SysResult.success(pageResult);
    }
    //修改用户状态
    @PutMapping("/status/{id}/{status}")
    public SysResult updateUserStatus(@PathVariable Integer id,@PathVariable Boolean status){
        userService.updateUserStatus(id,status);
        return SysResult.success();
    }
    //新增用户
    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        userService.addUser(user);
        return SysResult.success();
    }
    //根据用户ID更新数据
    @GetMapping("/{id}")
    public SysResult getUserById(@PathVariable Integer id){
        User user=userService.getUserById(id);
        if (user==null ||"".equals(user)) return SysResult.fail();
        return SysResult.success(user);
    }
    //根据用户id修改数据
    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }
    @DeleteMapping("/{id}")
    public SysResult deleteUser(@PathVariable Integer id){
        userService.deleteUser(id);
        return SysResult.success();
    }
}
